package com.tacc.weixin.api;

import com.tacc.tyun.util.DateUtil;
import com.tacc.weixin.model.TemplateMessage;
import com.tacc.weixin.model.info.AccessToken;
import com.tacc.weixin.model.info.OAuthAccessToken;
import com.tacc.weixin.model.info.User;
import com.tacc.weixin.util.WxUrlUtil;
import com.tacc.weixin.util.httpclient.HttpMethod;
import com.tacc.weixin.util.httpclient.HttpUtil;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import java.util.Date;

/**
 * Created by bingcai(bingly@foxmail.com) on 2017/6/22 0022.
 *
 */
public class WxApi {

    //获取接口访问凭证
    public static AccessToken getAccessToken(String appId, String appSecret) {
        AccessToken token = null;
        String tockenUrl = WxUrlUtil.getTokenUrl(appId, appSecret);
        JSONObject jsonObject = HttpUtil.httpRequestAndReturnResult(tockenUrl, HttpMethod.GET, null);
        if (null != jsonObject && !jsonObject.containsKey("errcode")) {
            try {
                token = new AccessToken();
                token.setExpiresIn(jsonObject.getInt("expires_in"));
                token.setEndTime(DateUtil.getSecondOneDate(new Date(),token.getExpiresIn()-180));//提前3分钟结束
                token.setAccessToken(jsonObject.getString("access_token"));
            } catch (JSONException e) {
                token = null;//获取token失败
            }
        }else if(null != jsonObject){
            token = new AccessToken();
            token.setErrcode(jsonObject.getInt("errcode"));
        }
        return token;
    }
    //获取OAuth2.0 Token
    public static OAuthAccessToken getOAuthAccessToken(String appId, String appSecret, String code) {
        OAuthAccessToken token = null;
        String tockenUrl = WxUrlUtil.getOAuthTokenUrl(appId, appSecret, code);
        JSONObject jsonObject = HttpUtil.httpRequestAndReturnResult(tockenUrl, HttpMethod.GET, null);
        if (null != jsonObject && !jsonObject.containsKey("errcode")) {
            try {
                token = new OAuthAccessToken();
                token.setOauthAccessToken(jsonObject.getString("access_token"));
                token.setExpiresIn(jsonObject.getInt("expires_in"));
                token.setOpenid(jsonObject.getString("openid"));
                token.setScope(jsonObject.getString("scope"));
            } catch (JSONException e) {
                token = null;//获取token失败
            }
        }else if(null != jsonObject){
            token = new OAuthAccessToken();
            token.setErrcode(jsonObject.getInt("errcode"));
        }
        return token;
    }
    //获取OAuth2.0 UserInfo
    public static User getOAuthUserInfo(OAuthAccessToken oAuthAccessToken) {
        User user = null;
        String oAuthUserinfoUrl = WxUrlUtil.getOAuthUserinfoUrl(oAuthAccessToken.getOauthAccessToken(),oAuthAccessToken.getOpenid());
        JSONObject jsonObject = HttpUtil.httpRequestAndReturnResult(oAuthUserinfoUrl, HttpMethod.GET, null);
        if (null != jsonObject && !jsonObject.containsKey("errcode")) {
            try {
                user = new User();
                user.setOpenid(jsonObject.getString("openid"));
                user.setNickname(jsonObject.getString("nickname"));
                user.setHeadlogo(jsonObject.getString("headimgurl"));
            } catch (JSONException e) {
                user = null;//获取token失败
            }
        }
        return user;
    }
    /**
     * 发送模板消息
     * @return
     */
    public static JSONObject sendTemplateMessage(TemplateMessage tplMsg, String accessToken){
        if(tplMsg != null){
            return HttpUtil.httpRequestAndReturnResult(WxUrlUtil.getSendTemplateMessageUrl(accessToken), HttpMethod.POST, tplMsg.toString());
        }
        return null;
    }

}
